package com.learner.exam.controller;

import com.learner.exam.service.QuestionService;
import com.learner.pojo.exam.Question;
import com.learner.pojo.exam.vo.PageResponse;
import com.learner.pojo.exam.vo.QuestionVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

/**
 * @author linweibing
 * @date 2023-03-11 0:45
 * @description: TODO
 */
@RestController
@RequestMapping("/api/exam/question")
public class QuestionController {

    @Autowired
    private QuestionService questionService;

    @GetMapping("/{id}")
    public QuestionVo getQuestionById(@PathVariable("id") Integer id) {
        return questionService.getQuestionVoById(id);
    }

    @GetMapping("/list")
    public PageResponse<QuestionVo> getQuestionByIds(@RequestParam List<Integer> ids) {
        return questionService.getQuestionVoByIds(ids);
    }


    /**
     * 获取题目信息,可分页 ----> 查询条件(可无)(questionType,questionBank,questionContent),必须有的(pageNo,pageSize)
     *
     * @param questionType    题目类型
     * @param questionBank    题库
     * @param questionContent 题目内容
     * @param page            页
     * @param pageSize        页大小
     * @return 结果
     */
    @GetMapping("/getQuestion")
    public PageResponse<Question> getQuestion(@RequestParam(required = false) String questionType,
                                              @RequestParam(required = false) String questionBank,
                                              @RequestParam(required = false) String questionContent,
                                              Integer page, Integer pageSize) {
        return questionService.getQuestion(questionType, questionBank, questionContent, page, pageSize);
    }

    /**
     * 根据id批量删除
     *
     * @param questionIds 问题id的字符串以逗号分隔
     */
    @GetMapping("/deleteQuestion")
    public void deleteQuestion(String questionIds) {
        questionService.deleteQuestionByIds(questionIds);
    }

    /**
     * 添加题目
     *
     * @param questionVo 题目
     */
    @PostMapping("/addQuestion")
    public void addQuestion(@RequestBody @Valid QuestionVo questionVo) {
        questionService.addQuestion(questionVo);
    }

    /**
     * 更新试题
     *
     * @param questionVo 题目
     */
    @PostMapping("/updateQuestion")
    public void updateQuestion(@RequestBody @Valid QuestionVo questionVo) {
        questionService.updateQuestion(questionVo);
    }
}
